/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.workflow.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.workflow.domain.ActReModel;
import me.zhengjie.modules.workflow.service.ActReModelService;
import me.zhengjie.modules.workflow.service.WorkFlowService;
import me.zhengjie.modules.workflow.service.dto.ActReModelQueryCriteria;
import me.zhengjie.modules.workflow.vo.ModelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author wzp
* @date 2020-10-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "流程模型管理")
@RequestMapping("/api/actReModel")
public class ActReModelController {

    private final ActReModelService actReModelService;

    @Autowired
    private WorkFlowService workFlowService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('actReModel:list')")
    public void download(HttpServletResponse response, ActReModelQueryCriteria criteria) throws IOException {
        actReModelService.download(actReModelService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询流程模型")
    @ApiOperation("查询流程模型")
    @PreAuthorize("@el.check('actReModel:list')")
    public ResponseEntity<Object> query(ActReModelQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(actReModelService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增流程模型")
    @ApiOperation("新增流程模型")
    @PreAuthorize("@el.check('actReModel:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ActReModel resources){
        return new ResponseEntity<>(actReModelService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改流程模型")
    @ApiOperation("修改流程模型")
    @PreAuthorize("@el.check('actReModel:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ActReModel resources){
        actReModelService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除流程模型")
    @ApiOperation("删除流程模型")
    @PreAuthorize("@el.check('actReModel:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        actReModelService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/getModel")
    @Log("查询流程模型")
    @ApiOperation("查询流程模型")
    @PreAuthorize("@el.check('actReModel:list')")
    public ResponseEntity<Object> getModel(String modelId){
        return new ResponseEntity<>(actReModelService.getModel(modelId),HttpStatus.OK);
    }

    @AnonymousAccess
    @PostMapping(value = "createModel")
    @Log("创建模型")
    @ApiOperation("保存流程设计模型数据")
    @PreAuthorize("@el.check('actReModel:add')")
    public void createModel(@Validated @RequestBody ModelVo modelVo){
        actReModelService.createModel(modelVo);
    }

    @Log("部署流程")
    @ApiOperation("部署流程")
    @PutMapping(value = "/deploy/{id}")
    @PreAuthorize("@el.check('actReModel:edit')")
    public ResponseEntity<Object> deploy(@PathVariable String id){
        workFlowService.deploy(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/getAllModels")
    @Log("获取全部模型")
    @ApiOperation("获取全部模型")
    @PreAuthorize("@el.check('actReModel:list')")
    public ResponseEntity<Object> getAllCategorys(ActReModelQueryCriteria criteria){
        return new ResponseEntity<>(actReModelService.queryAll(criteria),HttpStatus.OK);
    }
}